package com.njlime.cms.repository.sys;

import com.njlime.cms.entities.Live;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import com.njlime.sys.security.util.UserUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 直播
 * Created by Dean on 2017/8/11.
 */
@Repository
public class LiveDaoImpl extends BaseDaoImpl<Live> {

    /**
     * 更新
     *
     * @param live
     * @return
     */
    public int editLive(Live live) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_live set update_time = now(),updater = ?  ");
        arrList.add(live.getUpdater());
        if (!StringUtil.isEmpty(live.getTitle())) {
            sb.append(", title = ? ");
            arrList.add(live.getTitle());
        }
        if (!StringUtil.isEmpty(live.getSurface_plot())) {
            sb.append(", surface_plot = ? ");
            arrList.add(live.getSurface_plot());
        }
        if (!StringUtil.isEmpty(live.getSurface_plot())) {
            sb.append(", abstract_info = ? ");
            arrList.add(live.getAbstract_info());
        }
        if (live.getActivity_id() != null) {
            sb.append(", activity_id = ? ");
            arrList.add(live.getActivity_id());
        }
        if (live.getPosition() != null) {
            sb.append(", position = ? ");
            arrList.add(live.getPosition());
        }
        if (live.getLive_start_time() != null) {
            sb.append(", live_start_time = ? ");
            arrList.add(live.getLive_start_time());
        }
        if (live.getLive_end_time() != null) {
            sb.append(", live_end_time = ? ");
            arrList.add(live.getLive_end_time());
        }
        if (live.getLabel_id() != null) {
            sb.append(",label_id = ? ");
            arrList.add(live.getLabel_id());
        }
        if (live.getChannel_id() != null) {
            sb.append(",channel_id = ? ");
            arrList.add(live.getChannel_id());
        }
        if (live.getColumn_id() != null) {
            sb.append(",column_id = ? ");
            arrList.add(live.getColumn_id());
        }
        if (!StringUtil.isEmpty(live.getActor())) {
            sb.append(", actor = ? ");
            arrList.add(live.getActor());
        }
        if (!StringUtil.isEmpty(live.getMobiles())) {
            sb.append(", mobiles = ? ");
            arrList.add(live.getMobiles());
        }
        if (!StringUtil.isEmpty(live.getSource_url())) {
            sb.append(", source_url = ? ");
            arrList.add(live.getSource_url());
        }
        if (!StringUtil.isEmpty(live.getThird_url())) {
            sb.append(", third_url = ? ");
            arrList.add(live.getThird_url());
        }
        if (live.getPublish_status() != null) {
            sb.append(", publish_status = ? ");
            arrList.add(live.getPublish_status());
        }
        if (live.getIs_comment() != null) {
            sb.append(", is_comment = ? ");
            arrList.add(live.getIs_comment());
        }
        sb.append(" where id = ?");
        arrList.add(live.getId());
        return updateJdbc(sb.toString(), arrList);
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int del(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_live set is_del = 1 where id = ?");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    public PageBean list(PageBean pageBean, Live live) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
//        live_status 1 未开始 2 已结束 3 正在
        sb.append("select liv.*,tl.name label_name,tu.unit_name,u.name creator_name,tc.channel_name channel_name,tco.column_name column_name " +
                " from t_live liv " +
                " LEFT JOIN t_label tl ON tl.id = liv.label_id " +
                " LEFT JOIN t_unit tu ON liv.unit_id = tu.id" +
                " LEFT JOIN t_user u ON liv.creator = u.id" +
                " LEFT JOIN t_channel tc ON liv.channel_id = tc.id" +
                " LEFT JOIN t_column tco ON liv.column_id = tco.id");
        sb.append(" where liv.is_del !=1 ");
        if (!StringUtil.isEmpty(live.getTitle())) {
            sb.append(" and liv.title like concat('%',?,'%')");
            arrList.add(live.getTitle());
        }
        if (live.getChannel_id() != null) {
            sb.append(" and liv.channel_id = ? ");
            arrList.add(live.getChannel_id());
        }
        if (live.getPublish_status() != null) {
            sb.append(" AND liv.publish_status = ? ");
            arrList.add(live.getPublish_status());
        }
        if (live.getIs_comment() != null) {
            sb.append(" AND liv.is_comment = ? ");
            arrList.add(live.getIs_comment());
        }
        if (live.getLabel_id() != null) {
            sb.append(" and liv.label_id = ? ");
            arrList.add(live.getLabel_id());
        }
        if (live.getPosition() != null) {
            sb.append(" and liv.position = ? ");
            arrList.add(live.getPosition());
        }
        dataScopeFilter(UserUtils.getUser().getId(), sb, arrList, "tu", "liv");
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by liv.id desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 改变阅读数
     *
     * @return
     */
    public int updateReading(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" update t_live set reading = reading + 1 where id = ?");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 改变评论数量
     *
     * @return
     */
    public void updateCommentNum(Integer id, Integer num) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" update t_live set comment_num = comment_num + ? where id = ?");
        arrList.add(num);
        arrList.add(id);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 评论开关
     *
     * @param id
     * @return
     */
    public int toggleComment(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_live set is_comment = 1 - is_comment where id = ? ");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

}
